package com.woniu.cms.contract.core.controller;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.cms.contract.core.entity.AreaVo;
import com.woniu.cms.contract.core.entity.Bid;
import com.woniu.cms.contract.core.entity.BidResultManagement;
import com.woniu.cms.contract.core.entity.BidResultVo;
import com.woniu.cms.contract.core.service.IBidService;
import com.woniu.cms.contract.core.service.impl.BidServiceImpl;
import com.woniu.common.api.Result;
import com.woniu.common.api.status.Impl.BusinessCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author maple leaf
 * @since 2022-09-17
 */
@RestController
@RequestMapping("/bid")
@Api(tags = "投标创建接口")
public class BidController {

    @Autowired
    private IBidService bidService;


    @ApiOperation("新增投标单(未提交)")
    @PostMapping
    public Result addBid(@RequestBody Bid bid){
        //自动生成提交时间
        bid.setSubmitTime(new Date());

        bid.setManagers("李某某");

        //设置时间格式，截取时间年份
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        //将时间转成String
        String date = format.format(new Date());
        //根据用户输入地区，查询地区的编号
        String place = bid.getPlace();
        AreaVo areaVo = bidService.areaName(place);

        //截取编号后四位
        String substring = areaVo.getAreaId().substring(2, 6);

        //使用年份+编号进行模糊查询，得到list集合
        List<Bid> bids = bidService.generatedNumber("BID",date, substring);
        //判断集合是否有数据
        if (bids.size() == 0){
            //数据库没有数据则自添一条
            bid.setBidCode("BID" + date + substring + "001");
            System.out.println(bid.getBidCode());
        }else{
            //拿到集合的第一条数据
            Bid bid1 = bids.get(0);
            System.out.println("s = " + bid1);
            //拿到数据中最新的投标编号
            String bidCode = bid1.getBidCode();
            System.out.println("s1 = " + bidCode);
            //截取字段进行增加
            String substring1 = bidCode.substring(9, bidCode.length());
            Long i = Long.valueOf(Integer.parseInt(substring1));
            ++i;
            System.out.println("i = " + i);
            String s = String.valueOf(i);
            //截取字段，进行合并，添加到对象中
            String substring2 = bidCode.substring(0, 10);
            String s2 = substring2 + s;
            System.out.println("s2 = " + s2);
            bid.setBidCode(s2);
        }

        //添加数据到数据表中
        boolean b = bidService.save(bid);

        return b == true ? Result.ok() : Result.error();
    }

    @ApiOperation("保存并提交")
    @PostMapping("Submit")
    public Result addBidSubmit(@RequestBody Bid bid){
        bid.setState(1);
        Result result = addBid(bid);
        return result;
    }

    @ApiOperation("提交投标单")
    @PostMapping("Submit/{bidCode}")
    public Result bidSubmit(@PathVariable String bidCode){
        QueryWrapper<Bid> wrapper = new QueryWrapper<>();
        wrapper.eq("bid_code",bidCode);
        wrapper.like("state",0);
        Bid bid = new Bid();
        bid.setState(1);
        boolean b = bidService.update(bid, wrapper);

        return b == true ? Result.ok() :Result.error();

    }


    @ApiOperation("搜索投标单")
    @GetMapping
    public Result queryBid(String bidCode, String managers, Integer currentPage,Integer pageSize){
        //分页
        Page<Bid> page = new Page<>(currentPage, pageSize);
        //筛选条件
        QueryWrapper<Bid> wrapper = new QueryWrapper<>();

        if (bidCode != null && bidCode != ""){
            wrapper.like("bid_code",bidCode);
        }
        if (managers != null && managers != ""){
            wrapper.like("managers",managers);
        }

        List list = new ArrayList();
        Collections.addAll(list,0,1);
        System.out.println("list = " + list);
        wrapper.in("state",list);
        //分页查询
        Page<Bid> bidPage = bidService.page(page,wrapper);
        //拿到数据
        List<Bid> records = bidPage.getRecords();

        //拿到数据条数
        long total = bidPage.getTotal();

        //添加到map集合中
        HashMap<String, Object> map = new HashMap<>();
        map.put("records",records);
        map.put("total", total);
        //返回前端
        return Result.common(map);
    }

    @ApiOperation("修改的查询")
    @GetMapping("{bidCode}")
    public Result upQuery(@PathVariable String bidCode){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("bid_code",bidCode);
        Bid one = bidService.getOne(queryWrapper);
        return Result.common(one);
    }

    @ApiOperation("修改投标单")
    @PutMapping
    public Result upDataBid(@RequestBody Bid bid){

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("bid_code",bid.getBidCode());
        wrapper.eq("state",0);

        Bid one = bidService.getOne(wrapper);
        boolean success = false;
        if (one != null){
            success = bidService.updateById(bid);
        }

        return success == true ? Result.ok() : Result.error();
    }


    @ApiOperation("删除投标单")
    @DeleteMapping("{bidCode}")
    public Result deleBid(@PathVariable String bidCode){
        QueryWrapper<Bid> wrapper = new QueryWrapper<>();
        wrapper.eq("bid_code",bidCode);
        wrapper.eq("state",0);
        boolean remove = bidService.remove(wrapper);

        return remove == true ? Result.ok() : Result.error();

    }



}

